using System;
using System.Collections.Generic;
using System.Text;

namespace Forecast.Database
{
    public enum OfferAction
    {
        Buy,
        Sell,
        Cancel,
    }

    public class Offer
    {
        #region Enums

        public enum ColumnName
        {
            None,
            ID,
            Player_id,
            Prediction_id,
            Num_predictions,
            Price,
            Is_buy,
            Is_active,
            End_date,
            Is_accepted,
            Acceptor
        }

        #endregion
        #region Variables

        public static string TABLE_NAME = "Offer";

        private int _id;
        private int _playerId;
        private int _predictionId;
        private int _price;
        private bool _isBuy;
        private bool _isActive;
        private DateTime _endDate;
        private int _numPredictions;
        private bool _isAccepted;
        private int _acceptorId;

        #endregion
        #region Properties

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public int PlayerId
        {
            get { return _playerId; }
            set { _playerId = value; }
        }

        public int PredictionId
        {
            get { return _predictionId; }
            set { _predictionId = value; }
        }

        public int Price
        {
            get { return _price; }
            set { _price = value; }
        }

        public bool IsBuy
        {
            get { return _isBuy; }
            set { _isBuy = value; }
        }

        public bool IsActive
        {
            get { return _isActive; }
            set { _isActive = value; }
        }

        public DateTime EndDate
        {
            get { return _endDate; }
            set { _endDate = value; }
        }

        public int NumPredictions
        {
            get { return _numPredictions; }
            set { _numPredictions = value; }
        }

        public bool IsAccepted
        {
            get { return _isAccepted; }
            set { _isAccepted = value; }
        }

        public int AcceptorId
        {
            get { return _acceptorId; }
            set { _acceptorId = value; }
        }

        public OfferAction GetDefaultActionForPlayer(Player acceptor)
        {
            if (PlayerId == acceptor.Id)
                return OfferAction.Cancel;
            else if (IsBuy)
                return OfferAction.Sell;
            else
                return OfferAction.Buy;
        }

        public bool CanPerformDefaultAction (Player acceptor, Holding acceptorHolding)
        {
            if (PlayerId == acceptor.Id)
                return true; //cancel action
            else if (IsBuy)
            {
                //for now only full purchase is supported, that's why check is like this
                return acceptorHolding.PredictionId == PredictionId &&
                    acceptorHolding.NumPredictions >= NumPredictions;
            }
            else
                return acceptor.Cash >= NumPredictions*Price;
        }

        #endregion
    }
}
